package com.CmJava.serviceImpl;


import com.CmJava.entity.Friend;
import com.CmJava.entity.User;
import com.CmJava.entity.UserDetail;
import com.CmJava.mapper.FriendMapper;
import com.CmJava.mapper.UserDetailMapper;
import com.CmJava.service.UserService;
import com.CmJava.mapper.UserMapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Autowired
    private FriendMapper friendMapper;


    @Override
    public boolean register(String username, String password) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);

        QueryWrapper<UserDetail> userDetailQueryWrapper = new QueryWrapper<>();
        userDetailQueryWrapper.eq("username",username);

        QueryWrapper<Friend> friendQueryWrapper = new QueryWrapper<>();
        friendQueryWrapper.eq("username",username);

        User user = userMapper.selectOne(userQueryWrapper);
        UserDetail userDetail = userDetailMapper.selectOne(userDetailQueryWrapper);
        Friend friend = friendMapper.selectOne(friendQueryWrapper);

        if (user!=null || userDetail!=null || friend!=null){
            return false;
        }

        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        userMapper.insert(new User(0,username,password,"user","--",format));

        userDetailMapper.insert(new UserDetail(0,username,null,null,null,null,null,null,null));

        friendMapper.insert(new Friend(0,username,null,null,null));

        return true;
    }

    @Override
    public User getUser(String userName) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("userName", userName);
        User user = userMapper.selectOne(userQueryWrapper);
        if (user != null) {
            return user;
        } else {
            return null;
        }
    }

    @Override
    public List<User> getUserBlur(String username) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("username",username);
        List<User> user = userMapper.selectList(userQueryWrapper);
        if (user != null) {
            return user;
        } else {
            return null;
        }
    }

    @Override
    public User getUserById(String userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public boolean addFriend(String userName, String targetName) {
        User user = getUser(userName);
        User targetUser = getUser(targetName);
        String friendList0 = user.getFriendList();
        String friendList1 = targetUser.getFriendList();

        for (String userId : friendList0.split(",")) {
            if (userId.equals(String.valueOf(targetUser.getId()))){
                return false;
            }
        }

        try {

            if (friendList0.equals("--")) {
                user.setFriendList(String.valueOf(targetUser.getId()));
            } else {
                user.setFriendList(user.getFriendList() + "," + targetUser.getId());
            }

            if (friendList1.equals("--")) {
                targetUser.setFriendList(String.valueOf(user.getId()));
            } else {
                targetUser.setFriendList(targetUser.getFriendList() + "," + user.getId());
            }

            userMapper.updateById(user);
            userMapper.updateById(targetUser);

            //更新一下friend表
            friendMapper.insert(new Friend(0,userName,targetName,targetName,null));
            friendMapper.insert(new Friend(0,targetName,userName,userName,null));

        } catch (Exception e) {
            return false;
        }

        return true;
    }

    @Override
    public boolean deleteFriend(String username, String targetName) {
        User user = getUser(username);
        User targetUser = getUser(targetName);
        String friendList0 = user.getFriendList();
        String friendList1 = targetUser.getFriendList();
        if (friendList0.equals("--") || friendList1.equals("--")  ) {
            return false;
        }
        try {
            int userId = user.getId();
            int targetUserId = targetUser.getId();
            if (friendList0.contains(",")) {
                user.setFriendList(friendList0.substring(0,friendList0.length()-String.valueOf(targetUserId).length()-1));
            } else {
                user.setFriendList("--");
            }

            if (friendList1.contains(",")) {
                targetUser.setFriendList(friendList1.substring(0,friendList0.length()-String.valueOf(userId).length()-1));
            } else {
                targetUser.setFriendList("--");
            }
            userMapper.updateById(user);
            userMapper.updateById(targetUser);

            //更新一下friend表
            QueryWrapper<Friend> friendQueryWrapper = new QueryWrapper<>();
            friendQueryWrapper.eq("userName",username);
            friendQueryWrapper.eq("friendName",targetName);
            friendMapper.delete(friendQueryWrapper);

            QueryWrapper<Friend> friendQueryWrapper1 = new QueryWrapper<>();
            friendQueryWrapper1.eq("userName",targetName);
            friendQueryWrapper1.eq("friendName",username);
            friendMapper.delete(friendQueryWrapper1);

        } catch (Exception e) {
            return false;
        }

        return true;
    }
}
